﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.MapEditor.Controls
{
	public partial class Properties : UserControl
	{
		public Properties()
		{
			InitializeComponent();
		}

		EditorState EditorState;
        Engine.Selection Selected;
        Object SelectedObject;
		bool Changes = false;
		Dictionary<String, Object> OldValues;
		
		internal void SetEditorState(EditorState ES)
		{
			this.EditorState = ES;
			ES.OnSelection += OnSelected;
		}

		void OnSelected()
		{
            Object NewSelection = EditorState.GetSelectionObject(EditorState.Selected);
            Selected = EditorState.Selected;

            if (NewSelection == null)
                NewSelection = EditorState.Map.Header;

            TypeNameLabel.Text = NewSelection.GetType().Name;
            propertyGrid.SelectedObject = NewSelection;

            if (NewSelection != SelectedObject)
            {
                if (SelectedObject != null && Changes)
                    EditorState.PerformOperation(new Operations.PropertyChanged(SelectedObject, OldValues,
                        Operations.PropertyChanged.GetPropertySet(SelectedObject)));

                if (NewSelection != null)
                    OldValues = Operations.PropertyChanged.GetPropertySet(NewSelection);

                Changes = false;
            }
            SelectedObject = NewSelection;
		}

		private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
		{
			Changes = true;
		}
	}
}
